package com.ship.controller;

import com.ship.commens.Result;
import com.ship.dto.DeptDto;
import com.ship.dto.DeptSearchDto;
import com.ship.dto.UserDto;
import com.ship.dto.UserSearchDto;
import com.ship.service.DeptService;
import com.ship.service.UserService;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/dept")
public class DeptController {

    @Resource
    private DeptService deptService;

    @RequestMapping("/getAllDepts.do")
    public Result getAllDepts(){
        return deptService.findAllDepts();
    }

    //分页查询
    @RequestMapping("/getDeptsByPage.do")
    public Result getDeptsByPage(
            @RequestParam(required = false,defaultValue = "10") Integer pageSize,
            @RequestParam(required = false,defaultValue = "1") Integer pageNum
    ){
        return deptService.findDeptsByPage(pageNum,pageSize);
    }

    //添加部门
    @RequestMapping("/addDepts.do")
    public Result addDepts(@Validated DeptDto deptDto, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return Result.DATA_FORMAT_ERROR;
        }

        return deptService.saveDepts(deptDto);
    }

    //单个删除部门
    @RequestMapping("/cutOneDept.do")
    public Result cutOneDept(Long deptId){
        if (deptId==null || deptId<1){
            return Result.DATA_FORMAT_ERROR;
        }
        return deptService.removeDeptById(deptId);
    }

    //批量删除部门
    @RequestMapping("/cutManyDept.do")
    public Result cutManyDept(String deptIds){
        if (deptIds==null||!(deptIds+",").matches("([1-9][0-9]*,)+")){
            return Result.DATA_FORMAT_ERROR;
        }
        return deptService.removeMany(deptIds);
    }

    //多条件查询部门
    @RequestMapping("/getDeptsBySearch.do")
    public Result getDeptsBySearch(@Validated DeptSearchDto deptSearchDto, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return Result.DATA_FORMAT_ERROR;
        }

        return deptService.findDeptsBySearch(deptSearchDto);
    }

    //编辑部门信息
    @RequestMapping("/editDept.do")
    public Result editDept(@Validated DeptDto deptDto, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return Result.DATA_FORMAT_ERROR;
        }
        return deptService.modifyDept(deptDto);
    }

}
